<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>女性图片生成视频任务状态</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .status {
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .status-pending {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .status-running {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .status-completed {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .status-failed {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .log {
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 5px;
            padding: 15px;
            height: 300px;
            overflow-y: auto;
            font-family: monospace;
            white-space: pre-wrap;
            margin-bottom: 20px;
        }
        .video-link {
            display: inline-block;
            background-color: #28a745;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            margin-top: 10px;
            margin-right: 10px;
        }
        .video-link:hover {
            background-color: #218838;
        }
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
        .refresh-btn {
            background-color: #6c757d;
            color: white;
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-bottom: 10px;
        }
        .refresh-btn:hover {
            background-color: #5a6268;
        }
        .video-list {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>女性图片生成视频任务状态</h1>
        
        <div class="status status-{{ task.status }}">
            <h3>任务状态: 
                {% if task.status == 'pending' %}等待中
                {% elif task.status == 'running' %}处理中
                {% elif task.status == 'completed' %}已完成
                {% elif task.status == 'failed' %}失败
                {% else %}未知
                {% endif %}
            </h3>
            <p>{{ task.message }}</p>
        </div>
        
        {% if task.status == 'completed' and task.video_paths %}
        <div class="status status-completed">
            <h3>视频生成完成!</h3>
            <p>共生成 {{ task.video_paths|length }} 个视频:</p>
            <div class="video-list">
                {% for video_path in task.video_paths %}
                <a href="{{ url_for('download_video', filepath=video_path) }}" class="video-link" target="_blank">下载视频 {{ loop.index }}</a>
                {% endfor %}
            </div>
        </div>
        {% endif %}
        
        <button class="refresh-btn" onclick="refreshStatus()">刷新状态</button>
        
        <div class="log" id="logContent">{{ task.log or '等待日志...' }}</div>
        
        <a href="{{ url_for('female_portrait_video') }}" class="back-link">← 返回女性图片生成视频</a>
    </div>

    <script>
        function refreshStatus() {
            fetch('/api/female-video-task/{{ task.id }}')
                .then(response => response.json())
                .then(data => {
                    // 更新状态显示
                    const statusDiv = document.querySelector('.status');
                    statusDiv.className = 'status status-' + data.status;
                    statusDiv.innerHTML = '<h3>任务状态: ' + 
                        (data.status === 'pending' ? '等待中' :
                         data.status === 'running' ? '处理中' :
                         data.status === 'completed' ? '已完成' :
                         data.status === 'failed' ? '失败' : '未知') + 
                        '</h3><p>' + data.message + '</p>';
                    
                    // 更新日志
                    document.getElementById('logContent').textContent = data.log || '等待日志...';
                    
                    // 如果任务完成，显示视频链接
                    if (data.status === 'completed' && data.video_paths) {
                        const videoDiv = document.createElement('div');
                        videoDiv.className = 'status status-completed';
                        let videoLinks = '<h3>视频生成完成!</h3><p>共生成 ' + data.video_paths.length + ' 个视频:</p><div class="video-list">';
                        for (let i = 0; i < data.video_paths.length; i++) {
                            videoLinks += '<a href="/download?filepath=' + encodeURIComponent(data.video_paths[i]) + '" class="video-link" target="_blank">下载视频 ' + (i+1) + '</a>';
                        }
                        videoLinks += '</div>';
                        videoDiv.innerHTML = videoLinks;
                        document.querySelector('.container').insertBefore(videoDiv, document.querySelector('.refresh-btn').nextSibling);
                    }
                })
                .catch(error => {
                    console.error('获取状态失败:', error);
                });
        }
    </script>
</body>
</html>